/*Debug Log*/
function smb_WriteLog(handler, obj, log) {
    var description = obj;
    if (obj && typeof obj !== 'string')
        description = JSON.stringify(obj);
    var time = smb_DateToString(new Date(), 3);
    if (log) {
        smb_InsertIntoArray("$AppLogs", handler);
        smb_InsertIntoArray("$AppLog_" + handler, {
            time : time,
            description : "[" + handler + "]:" + description + ""
        });
        smb_CacheStorageKey("$AppLogs");
        smb_CacheStorageKey("$AppLog_" + handler);
    }
    console.log(time + "[" + handler + "]" + description);
}

function smb_ClearLogs(handler) {
    if (handler) {
        smb_ClearStorageKey("$AppLog_" + handler);
        smb_RemoveFromArray("$AppLogs", handler);
    } else {
        var arrHandler = smb_GetStorageArray("$AppLogs");
        if (arrHandler) {
            for (var i = 0; i < arrHandler.length; i++) {
                smb_ClearStorageKey("$AppLog_" + arrHandler[i]);
                smb_RemoveFromArray("$AppLogs", arrHandler[i]);
            }
        }
    }
}

function smb_ListLogs(handler) {
    var sJson = undefined;
    var list = undefined;
    if (handler) {
        sJson = appcan.locStorage.val("$AppLog_" + handler);
        if (sJson)
            list = JSON.parse(sJson);
        else
            list = [];
    } else {
        list = [];
        var arr = smb_GetStorageArray("$AppLogs");
        for (var i = 0; i < arr.length; i++) {
            var _handler = arr[i];
            sJson = appcan.locStorage.val("$AppLog_" + _handler);
            if (sJson) {
                var _list = JSON.parse(sJson);
                list = list.concat(_list);
            }
        }
        list.sort(function(a, b) {
            return a.time < b.time ? 1 : -1;
        });
    }
    return list;
}